package com.xianzhiOA.service.impl;

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzPlan;
import com.xianzhiOA.dao.XzEmpMapper;
import com.xianzhiOA.dao.XzPlanMapper;
import com.xianzhiOA.service.XzPlanService;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *PlanServiceImpl
 *
 * @author 甘汝雷
 * @create 2017-04-19 10:17
 **/
@Service("XzPlanService")
public class XzPlanServiceImpl implements XzPlanService {
    @Autowired
    private XzPlanMapper planMapper;
    @Autowired
    private XzProjectService projectService;
    @Autowired
    private XzWorkService workService;
    //分页查询
    @Override
    public PageBean<XzPlan> selAll(XzPlan plan, int page, int rows,  Date begina,Date beginb,String beginc,String endc) {
        PageHelper.startPage(page, rows);
        Map map = myMap(plan);
        if (begina!=null){
            map.put("beginDatea",begina);
            map.put("endDatea",workService.myTime(begina));
        }
        if (beginb!=null){
            map.put("beginDateb",beginb);
            map.put("endDateb",workService.myTime(beginb));
        }
        if (beginc!=null)map.put("beginDatec",projectService.myDate(beginc));
        if (endc!=null)map.put("endDatec",projectService.myDate(endc));
        List<XzPlan> plist = planMapper.selAll(map);
        PageBean<XzPlan> pb = new PageBean<>(plist);
        return pb;
    }
    //把plan转换成Map
    public Map<String, Object> myMap(XzPlan plan) {
        Map<String, Object> map = new HashMap<>();
        map.put("planId", plan.getPlanId());
        map.put("planProject", plan.getPlanProject());
        map.put("planName", plan.getPlanName());
        map.put("planRank", plan.getPlanRank());
        map.put("planStart", plan.getPlanStart());
        map.put("planEnd", plan.getPlanEnd());
        map.put("planDescribe", plan.getPlanDescribe());
        map.put("planSuper", plan.getPlanSuper());
        map.put("field1", plan.getField1());
        map.put("field2", plan.getField2());
        map.put("field3", plan.getField3());
        map.put("project", plan.getProject());
        map.put("isdelete", plan.getIsdelete());
        map.put("projectName", plan.getProjectName());
        map.put("audit", plan.getAudit());
        map.put("official", plan.getOfficial());
        map.put("superName", plan.getSuperName());
        return map;
    }
    //通过计划名查询数据库
    @Override
    public List<XzPlan>selAll(XzPlan plan){
        return planMapper.selAll(myMap(plan));
    }

    //通过id进行查询
    @Override
    public XzPlan selById(Long planId) {
        return planMapper.selById(planId);
    }

    //添加计划
    @Override
    public int addPlan(XzPlan plan) {
        return planMapper.insert(plan);
    }
    //删除计划
    @Override
    public int delete(Long planId){
        return planMapper.delete(planId);
    }

    @Override
    public int isdelete(Long planId) {
        Map map = new HashMap();
        map.put("planId",planId);
        map.put("isdelete",1);
        return planMapper.update(map);
    }

    //修改计划
    @Override
    public int update(XzPlan plan){
        return planMapper.update(myMap(plan));
    }

    @Override
    public XzPlan selByName(Map map) {
        return planMapper.selByName(map);
    }
    @Override
    public List<XzPlan> selByPlan(Map map) {
        return planMapper.selByPlan(map);
    }
}
